<?php
/**
 * Created by PhpStorm.
 * User: tom_xia
 * Date: 2018/7/2
 * Time: 14:56
 * 视频vv统计
 */
require_once dirname(__DIR__)."/common.php";
use app\lib\db\DbException;
use app\cls\tom\Queue;
use app\cls\tom\SysResource;
use app\cls\tom\Process;

$process = new Process(__FILE__);
$process->setProcessNum(1);
$process->initProcess();
$process->isTryError = false;
$redis = SysResource::getRedis();
$db =  SysResource::getDB(true);
$queue = new Queue($redis,REDIS_PREFIX."video_vv");
$queueNum = $queue->getQueueNum();
try{
    while(true){
        for($i = 0 ;$i< $queueNum;$i++){
            for($j = 0 ;$j < 100;$j++){
                $dataStr = $queue->pop($i);
                if(empty($dataStr)){
                    break;
                }
                echo $dataStr."\n";
                $data = json_decode($dataStr,true);
                if(is_array($data)){
                    if($data["type"] == 1){
                        $db->update("user_material",["vv_num"=>"!vv_num+1"],["user_material_uuid"=>$data["uuid"]]);
                    }else if($data["type"] == 2){
                        $db->update("video",["vv_num"=>"!vv_num+1"],["material_id"=>$data["uuid"]]);
                    }
                    $db->insert("vv",[
                        "uuid"=>$data["uuid"],
                        "type"=>$data["type"],
                        "channel_code"=>$data["channel_code"],
                        "add_time"=>$data["time"],
                    ]);
                }
            }
        }
        sleep(1);
        $db->reconnect();
        $process->checkProcessTime(true);
    }
}catch (DBException $e){
    if(!empty($dataStr)){
        $queue->push($dataStr,1,"right");
    }
    $process->log($e->getMessage());
    $process->restartProcess();
}catch (Exception $e){
    $process->log($e->getMessage());
}